/**
 * Created by gongxun on 17/1/19.
 */


var PdkTableEndLayer = GameCCSBase.extend({
    Panel_players : null,
    mRoundEndInfo : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/PdkTableEnd.json");
        this.Panel_players = [];

        //确定
        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (mo.pdkTable.mPlayerType == 0){
                    //录像x
                    this.setVisible(false);
                    mo.pdkTable.mPdkTableReplayLayer.onPlayEnd();
                }else{
                    if (mo.pdkTable.mPdkTableRoomEndLayer == null){
                        mo.gameMsgManager.sendGameReadyRequestpdk();
                    }else{
                        //打完了 显示大结算
                        mo.pdkTable.mPdkTableRoomEndLayer.setVisible(true);
                        this.setVisible(false);
                    }
                }
            }
        }, this);

        this.loadUI();

        this.setVisible(false);
    },

    loadUI : function(){
        for (var i = 0; i < 3; i++){
            var Panel_player = mo.seekWidgetByName(this.ccsNode, "Panel_player" + i);
            this.Panel_players.push(Panel_player);
        }
    },

    setVisible : function(isVisible){
        this.unschedule(this.updateEndInfo);
        this._super(isVisible);
    },

    setEndInfo : function(roundEndInfo, delayTime){
        this.mRoundEndInfo = roundEndInfo;

        this.scheduleOnce(function(){
            this.updateEndInfo();
        }, delayTime);
    },

    updateEndInfo : function(){
        this.setVisible(true);
        mo.seekWidgetByName(this.ccsNode, "Panel_center").setScale(0);
        mo.seekWidgetByName(this.ccsNode, "Panel_center").runAction(cc.sequence(cc.scaleTo(0.3, 1.2), cc.scaleTo(0.1, 1.0)));
        var roundEndInfo = this.mRoundEndInfo;

        //自己的输赢
        var mySeatUser = roundEndInfo["SeatUser" + roundEndInfo.MySeat];
        var myids = mySeatUser.LostWin.ItemTypes;
        var mynums = mySeatUser.LostWin.ItemNums;
        var myallfen = mo.gameUserInfo.getItemNum(myids, mynums, 0);
        var myWin = myallfen >= 0;
        mo.gameHelper.loadTextureImage(mo.seekWidgetByName(this.ccsNode, "Image_winBg"), myWin ? "res/ccs/pdk/end/img_win.png" : "res/ccs/pdk/end/img_lose.png");
        mo.soundHelper.playPdkWin(myWin);


        var seatUsers = [];
        for (var seat = 0; seat < 3; seat++){
            var logicSeat = mo.pdkTableStatus.getLogicForView(seat, roundEndInfo.MySeat - 1);
            var seatUser = roundEndInfo["SeatUser" + (logicSeat + 1)];
            if(seatUser != null){
                seatUsers.push(seatUser);
            }
        }

        for (var seat = 0; seat < 3; seat++){
            var seatUser = seatUsers[seat];

            //
            var nickStr = "";
            var scoreStr = "";

            var Text_nick = mo.seekWidgetByName(this.Panel_players[seat], "Text_nick");
            var Image_head = mo.seekWidgetByName(this.Panel_players[seat], "Image_head");
            var Fnt_score = mo.seekWidgetByName(this.Panel_players[seat], "Fnt_score");
            var Text_bomb = mo.seekWidgetByName(this.Panel_players[seat], "Text_bomb");
            Text_bomb.setVisible(false);

            if(seatUser != null){
                this.Panel_players[seat].setVisible(true);
                nickStr = mo.gameHelper.getUserNickFilter(seatUser.user_info.nickname);

                var allfen = 0;
                var ids = seatUser.LostWin.ItemTypes;
                var nums = seatUser.LostWin.ItemNums;
                allfen = mo.gameUserInfo.getItemNum(ids, nums, 0);
                if (allfen >= 0){
                    Fnt_score.setFntFile("res/ccs/pdk/end/bigWinFnt.fnt");
                    allfen = "+" + allfen;
                }else {
                    Fnt_score.setFntFile("res/ccs/pdk/end/bigLoseFnt.fnt");
                }
                scoreStr = allfen;

                Image_head.setVisible(true);
                mo.gameHelper.loadImageForUrl(Image_head, seatUser.user_info.faceUrl);

                //炸弹
                var bombCount = this.getMyBombCount(roundEndInfo, seat);
                if (bombCount > 0){
                    Text_bomb.setVisible(true);
                    mo.gameHelper.setTextString(Text_bomb, "炸弹X" + bombCount);
                }
            }else{
                this.Panel_players[seat].setVisible(false);
                nickStr = "[空座位]";
                scoreStr = "0";

                Image_head.setVisible(false);
            }

            //nick
            mo.gameHelper.setTextString(Text_nick, nickStr);

            //杠分
            Fnt_score.setString(scoreStr);
        }
    },

    //返回自己炸弹的数量
    getMyBombCount : function(roundEndInfo, viewSeat){
        var bombCount = 0;

        var logicSeat = mo.pdkTableStatus.getLogicForView(viewSeat, mo.pdkTableStatus.mTableStatus.MySeat - 1);
        for (var i = 0; i < roundEndInfo.RunTimeResult.length; i++){
            var item = roundEndInfo.RunTimeResult[i];
            if (item.ToSeat == (logicSeat + 1) && item.ResultType == 1){
                bombCount++;
            }
        }

        bombCount = parseInt(bombCount / 2);
        return bombCount;
    }
});












